package com.example.config;


import com.example.common.json.JacksonObjectMapper;
import com.example.interceptor.JwtTokenAdminInterceptor;
import com.example.interceptor.JwtTokenUserInterceptor;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.servers.Server;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.customizers.OpenApiCustomizer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;
@Slf4j
@Configuration
public class OpenApiConfig implements WebMvcConfigurer {

    // OpenAPI类用于定制全局文档信息
    @Autowired
    private JwtTokenAdminInterceptor jwtTokenAdminInterceptor;

    @Autowired
    private JwtTokenUserInterceptor jwtTokenUserInterceptor;
    /**
     * 注册自定义拦截器
     *
     * @param registry
     */
    public void addInterceptors(InterceptorRegistry registry) {
        log.info("开始注册自定义拦截器...");
        registry.addInterceptor(jwtTokenAdminInterceptor)
                .addPathPatterns("/admin/**")
                .excludePathPatterns("/admin/employee/login");

        registry.addInterceptor(jwtTokenUserInterceptor)
//                .addPathPatterns("/user/**")
                .addPathPatterns("/user/none")
                .excludePathPatterns("/user/user/login")
                .excludePathPatterns("/user/shop/status");
    }
    @Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI()
                // 定制文档基本信息
                .info(new Info()
                        //关于文档信息
                        .title("小易外卖项目接口文档")//API 文档标题
                        .description("小易外卖项目接口文档")//API 文档描述
                        .version("2.0.0")
                        .termsOfService("https://example.com/terms")//API服务条款


                        //关于作者
                        .contact(new Contact()
                                .name("YuHang Chen")//开发者姓名
                                .url("https://example.com/contact")
                                .email("dev@example.com"))

                        //关于许可证
                        .license(new License()
                                .name("Apache 2.0")
                                .url("https://www.apache.org/licenses/LICENSE-2.0.html")))

                //配置服务器信息
                .servers(List.of(
                        new Server().url("http://localhost:8080").description("本地服务器"),
                        new Server().url("https://api.example.com").description("生产服务器")))

                //配置外部文档信息
                .externalDocs(new ExternalDocumentation()
                        .description("外部文档描述")
                        .url("https://example.com/docs"));
    }

    // GroupedOpenApi类用于API分组
//    @Bean
//    public GroupedOpenApi publicApi() {
//        return GroupedOpenApi.builder()
//                .group("public")
//                .pathsToMatch("/api/**")
//                .build();
//    }

    // OpenApiCustomizer类用于自定义OpenAPI对象，比如全局添加头部信息。
    @Bean
    public OpenApiCustomizer customerGlobalHeaderOpenApiCustomizer() {
        return openApi -> openApi.info(
                new Info().title("自定义 API 文档")
                        .description("自定义 API 文档描述")
                        .version("2.0.0")
        );
    }

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        //创建消息转换器对象
        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
        //设置对象转换器，底层使用Jackson将Java对象转为json
        messageConverter.setObjectMapper(new JacksonObjectMapper());
        //将上面的消息转换器对象追加到mvc框架的转换器集合中
        converters.add(3, messageConverter);
    }
}
